package com.example.ui.contact.db;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import com.example.ui.contact.bean.MessageItem;


import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;


public class MessageDB {
	public static final String MSG_DBNAME = "myMsg.db";
	private SQLiteDatabase db;

	public MessageDB(Context context) {
		db = context.openOrCreateDatabase(MSG_DBNAME, Context.MODE_PRIVATE,
				null);
	}
//    this.name = name;
//    this.date = date;
//    this.text = text;
//    this.isComMeg = isComMsg;
	
//	CREATE table IF NOT EXISTS MeMsg_TABLE_NAME
//	(
//		id INTEGER PRIMARY KEY AUTO_INCREMENT not null,
//		userId VARCHAR(20)
//	)
	
	public void saveMsg(String id, MessageItem entity) {
		db.execSQL("CREATE table IF NOT EXISTS _"
				+ id
				+ " (_id INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT, date TEXT,message TEXT,isComMeg TEXT)");
		int isCome = 0;
		if (entity.getMsgType()) {  // 如果是收到的消息，保存在数据库的值为1
			isCome = 1;
		}
		db.execSQL(
				"insert into _"
						+ id
						+ " (name,date,message,isCome) values(?,?,?,?)",
				new Object[] { entity.getName(),entity.getDate(),entity.getMsgText(),isCome});
	}

	public List<MessageItem> getMsg(String id, int pager) {
		List<MessageItem> list = new LinkedList<MessageItem>();
		int num = 10 * (pager + 1);// 本来是准备做滚动到顶端自动加载数据
		db.execSQL("CREATE table IF NOT EXISTS _"
				+ id
				+ " (_id INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT, date TEXT,message TEXT,isCome TEXT)");
		Cursor c = db.rawQuery("SELECT * from _" + id
				+ " ORDER BY _id DESC LIMIT " + num, null);
		while (c.moveToNext()) {
			String name = c.getString(c.getColumnIndex("name"));
			String date = c.getString(c.getColumnIndex("date"));
			String message = c.getString(c.getColumnIndex("message"));
			int isCome = c.getInt(c.getColumnIndex("isCome"));
			
			boolean isComMsg = false;
			if (isCome == 1) {
				isComMsg = true;
			}
			MessageItem entity =new MessageItem(name, date, message,isComMsg,"0");
			list.add(entity);
		}
		c.close();
		Collections.reverse(list);// 前后反转一下消息记录
		return list;
	}

	public int getNewCount(String id) {
		db.execSQL("CREATE table IF NOT EXISTS _"
				+ id
				+ " (_id INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT, date TEXT,message TEXT,isCome TEXT)");
		Cursor c = db.rawQuery("SELECT isNew from _" + id + " where isNew=1",
				null);
		int count = c.getCount();
		// L.i("new message num = " + count);
		c.close();
		return count;
	}

	public void clearNewCount(String id) {
		db.execSQL("CREATE table IF NOT EXISTS _"
				+ id
				+ " (_id INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT, date TEXT,message TEXT,isCome TEXT,isNew TEXT)");
		db.execSQL("update _" + id + " set isNew=0 where isNew=1");
	}

	public void close() {
		if (db != null)
			db.close();
	}
}
